package br.com.gc.rfqx.controller;

import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;

import br.com.gc.rfqx.model.entity.Customer;
import br.com.gc.rfqx.model.service.CustomerService;


@Controller("customerController")
@ManagedBean(name="customerController")
@ViewScoped
public class CustomerController {

	private Customer customer;
	
	private List<Customer> customers;

	@Autowired
	private CustomerService service;
	
	public CustomerController() {
		customer = new Customer();
	}
	
	@Transactional
	public void save() {
		
		System.out.println(customer);
		
		service.save(customer);
		
		FacesContext.getCurrentInstance()
			.addMessage(null, new FacesMessage("Caraca Maluco " + customer));
	}


	public List<Customer> getCustomers() {
		
		if (customers == null || customers.isEmpty()) {
			customers = service.list();
		}
		
		return customers;
		
	}
	
	public Customer getCustomer() {
		return customer;
	}


	public void setCustomer(Customer customer) {
		this.customer = customer;
	}
	
		
}
